import React from 'react';
import LeftMenu from '../Home/LeftMenu/index';
import Right from '../Home/Right/index';
import axios from '../../config/axios';
import {host} from '../../config/host';
import { connect } from "react-redux";
import { userActions } from '../../store/user';
import {withRouter } from 'react-router';

import './index.less';

class Home extends React.Component {
    constructor(props) {
        super(props);
        this.handleUserInfo = this.handleUserInfo.bind(this);
    }

    async handleUserInfo() {
        try {
            const msg = await axios.get(host + "login/info");
            if (msg.data.statusCode === 0) {
                this.props.history.push('/login');
            } else {
                const {changeUserId, changeOrg, changeRole, changePermission} = this.props;
                const data = JSON.parse(msg.data.statusMsg);
                const { userId, org, role, add, edit} = data;
                const deleteData = data.delete
                changeUserId({userId})
                changeOrg({org})
                changeRole({role})
                changePermission({
                    add, edit,
                    delete: deleteData
                })
            }
        } catch (e) {
            console.log(e)
            this.props.history.push('/login');
        }
    }

    componentDidMount() {
        this.handleUserInfo();
    }

    componentWillReceiveProps(nextProps, nextContext) {
        this.handleUserInfo();
    }

    render() {
        return (
            <div className="home">
                <LeftMenu />
                <Right/>
            </div>
        );
    }
}

export default withRouter(connect(null, userActions)(Home))